package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.*;
import cn.sxt.clinic.service.IDeptService;
import cn.sxt.clinic.service.IDictionaryService;
import cn.sxt.clinic.service.IDoctorService;
import cn.sxt.clinic.service.IJobService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Controller
@RequestMapping("/doctor")
public class DoctorController {

    @Autowired
    private IDoctorService doctorService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private IDictionaryService dictionaryService;
    @Autowired
    private IJobService jobService;

    /*页面跳转*/
    @RequiresPermissions("doctor:doctorManager")
    @RequestMapping("/doctorManager")
    public  String userManager(){

        return "doctorManager";
    }

    /*编辑页面跳转*/
    @RequestMapping("/doctorEdit")
    public String doctorManager(Long doctorId, Model m){


        //编辑页面数据共享
        //科室
        List<Dept> deptList = deptService.list();
        m.addAttribute("depts",deptList);
        //级别
        QueryWrapper<Dictionary> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",14);
        List<Dictionary> dictionaryList = dictionaryService.list(queryWrapper);
        m.addAttribute("dictionarys",dictionaryList);
        //职务
        List<Job> jobList = jobService.list();
        m.addAttribute("jobs",jobList);
        //编辑页面数据回显
        QueryWrapper<Doctor> wrapper=new QueryWrapper<>();
        wrapper.eq("doctor_id",doctorId);
        Doctor doctor = doctorService.getOne(wrapper);
        m.addAttribute("doctor",doctor);

        return "doctorEdit";
    }

    /*分页查询医生列表*/
    @RequiresPermissions("doctor:list")
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> doctorList(@RequestParam(defaultValue = "1")Integer pageNum,
                                          @RequestParam(defaultValue = "10")Integer pageSize,
                                          String keyword){
        //创建分页对象
        IPage iPage =new Page<>(pageNum,pageSize);
        //创建条件查询对象
        QueryWrapper<Doctor> queryWrapper=new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)){
            queryWrapper.like("realname",keyword);
            queryWrapper.or();
            queryWrapper.like("dept_name",keyword);
            queryWrapper.or();
            queryWrapper.like("job_name",keyword);
            queryWrapper.or();
            queryWrapper.like("level",keyword);
        }


        IPage doctorPage = doctorService.page(iPage, queryWrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",doctorPage.getTotal());
        map.put("rows",doctorPage.getRecords());

        return map;
    }

    //删除操作
    @RequiresPermissions("doctor:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public ResultCode delDoctor(Long doctorId){

        UpdateWrapper<Doctor> wrapper=new UpdateWrapper<>();
        wrapper.eq("doctor_id",doctorId);

        boolean remove = doctorService.remove(wrapper);
        ResultCode msg=new ResultCode();
        if (remove){
            msg.setCode(1);
            msg.setMsg("删除成功");
        }else {
            msg.setCode(0);
            msg.setMsg("删除失败");
        }
        return msg;
    }

    @RequiresPermissions("doctor:insert")
    @RequestMapping("/insert")
    @ResponseBody
    public ResultCode insertDoctor(Doctor doctor){

        doctor.setRegisterTime(LocalDate.now());
        //生成盐
        String salt = UUID.randomUUID().toString().substring(0, 5);
        doctor.setSalt(salt);
        //密码加密
        Md5Hash md5Hash = new Md5Hash(doctor.getPassword(), salt, 3);
        doctor.setPassword(md5Hash.toString());
        boolean update = doctorService.saveOrUpdate(doctor);
        ResultCode msg=new ResultCode();
        if (update){
            msg.setCode(1);
            msg.setMsg("添加医生成功");
        }else {
            msg.setCode(0);
            msg.setMsg("添加医生失败");
        }
        return msg;
    }
    @RequiresPermissions("doctor:update")
    @RequestMapping("/update")
    @ResponseBody
    public ResultCode updateDoctor(Doctor doctor){

        //生成盐
        String salt = UUID.randomUUID().toString().substring(0, 5);
        doctor.setSalt(salt);
        //密码加密
        Md5Hash md5Hash = new Md5Hash(doctor.getPassword(), salt, 3);
        doctor.setPassword(md5Hash.toString());
        boolean update = doctorService.saveOrUpdate(doctor);
        ResultCode msg=new ResultCode();
        if (update){
            msg.setCode(1);
            msg.setMsg("修改医生信息成功");
        }else {
            msg.setCode(0);
            msg.setMsg("修改医生信息失败");
        }
        return msg;
    }

}

